package com.example.lp.lpuicore.views.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;

import android.widget.TextView;

import com.example.lp.lpuicore.views.view.move.MoveActivity;
import com.example.lp.lpuicore.views.view.move.MoveView;
import com.example.lp.lpuicore.R;

public class ViewAboutActivity extends AppCompatActivity implements View.OnTouchListener {

    private static final String TAG = "ViewAboutActivity";
    private View view;
    private MoveView move_view;

    private TextView details1, details2, details3;
    private VelocityTracker velocityTracker;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_about);
        initViews();
    }

    private void initViews() {
        view = findViewById(R.id.view);
        details1 = findViewById(R.id.tv_details1);
        details2 = findViewById(R.id.tv_details2);
        details3 = findViewById(R.id.tv_details3);
        move_view = findViewById(R.id.mv_move);
        findViewById(R.id.btn_move).setOnClickListener(v -> {
            startActivity(new Intent(this, MoveActivity.class));
        });
        findViewById(R.id.clear).setOnClickListener(v -> {
            touchBuiuder1.delete(0, touchBuiuder1.length());
            touchBuiuder2.delete(0, touchBuiuder2.length());
            touchBuiuder3.delete(0, touchBuiuder3.length());
            details1.setText(touchBuiuder1.toString());
            details2.setText(touchBuiuder2.toString());
            details3.setText(touchBuiuder3.toString());
        });
        findViewById(R.id.btn_mode_layout).setOnClickListener(view -> move_view.switchMode(MoveView.MODE.MODE_LAYOUT));
        findViewById(R.id.btn_mode_offset).setOnClickListener(view -> move_view.switchMode(MoveView.MODE.MODE_OFFSET));
        findViewById(R.id.btn_mode_lp).setOnClickListener(view -> move_view.switchMode(MoveView.MODE.MODE_LAYOUTPARAMS));
        findViewById(R.id.btn_mode_scroll).setOnClickListener(view -> move_view.switchMode(MoveView.MODE.MODE_SCROLL));
        findViewById(R.id.btn_mode_amin).setOnClickListener(view -> move_view.switchMode(MoveView.MODE.MODE_ANIMATION));
        findViewById(R.id.btn_mode_scroller).setOnClickListener(view -> move_view.switchMode(MoveView.MODE.MODE_SCROLLER));
        findViewById(R.id.btn_hv).setOnClickListener(view -> startActivity(new Intent(this, HVActivity.class)));
        //增加触摸监听
        view.setOnTouchListener(this);
        gestureDetector = new GestureDetector(MyGesture);
        gestureDetector.setOnDoubleTapListener(MyDoubleGesture);
        //解决长按屏幕无法拖动
        gestureDetector.setIsLongpressEnabled(true);

    }

    private StringBuffer touchBuiuder1 = new StringBuffer();
    private StringBuffer touchBuiuder2 = new StringBuffer();
    private StringBuffer touchBuiuder3 = new StringBuffer();

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i(TAG, "-----onTouch-----");
        gestureDetector.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "-----ACTION_DOWN-----");
                touchBuiuder1.append("下按" + "\r\n");
                //赋值
                velocityTracker = VelocityTracker.obtain();
                break;
            case MotionEvent.ACTION_MOVE:
//                Log.i(TAG, "-----ACTION_MOVE-----");
                //计算速度
                velocityTracker.addMovement(event);
                velocityTracker.computeCurrentVelocity(100);
                float xVelocity = velocityTracker.getXVelocity();
                float yVelocity = velocityTracker.getYVelocity();
                touchBuiuder1.append("最小滑动间隙 ：" + ViewConfiguration.get(this).getScaledTouchSlop() + "\r\n" +
                        "速度X：" + xVelocity + " 个像素点/100ms" + "\r\n" +
                        "速度Y：" + yVelocity + " 个像素点/100ms");
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "-----ACTION_UP-----");
                touchBuiuder1.append("抬起" + "\r\n");
                //回收
                velocityTracker.clear();
                velocityTracker.recycle();
                break;
        }
        //如果返回为false 则只响应down事件
        details1.setText(touchBuiuder1.toString());
        details2.setText(touchBuiuder2.toString());
        details3.setText(touchBuiuder3.toString());
        return true;
    }

    private GestureDetector.OnDoubleTapListener MyDoubleGesture = new GestureDetector.OnDoubleTapListener() {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.i(TAG, "-----onSingleTapConfirmed-----");
            touchBuiuder3.append("OnDoubleTapListener 严格单击行为" + "\r\n");
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i(TAG, "-----onDoubleTap-----");
            touchBuiuder3.append("OnDoubleTapListener 两次连续的单击" + "\r\n");
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            Log.i(TAG, "-----onDoubleTapEvent-----");
            touchBuiuder3.append("OnDoubleTapListener 双击" + "\r\n");
            return false;
        }
    };
    private GestureDetector.OnGestureListener MyGesture = new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            Log.i(TAG, "-----onDown-----");
            touchBuiuder2.append("GestureDetector 下按" + "\r\n");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Log.i(TAG, "-----onShowPress-----");
            touchBuiuder2.append("GestureDetector 轻轻按屏幕没有松开" + "\r\n");
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.i(TAG, "-----onSingleTapUp-----");
            touchBuiuder2.append("GestureDetector 松开" + "\r\n");
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.i(TAG, "-----onScroll-----");
            touchBuiuder2.append("GestureDetector 拖动 distanceX  " + distanceX + "   distanceY" + distanceY + "\r\n");
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.i(TAG, "-----onLongPress-----");
            touchBuiuder2.append("长按" + "\r\n");
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.i(TAG, "-----onFling-----");
            touchBuiuder2.append("按下屏幕 快速松开" + "\r\n");
            return false;
        }
    };
}
